let tools = {

	getRootDomain: function() {
		var host = window.location.hostname;
		return host.split('.').slice(-2).join('.');
	},

	getLocalStorage: function(name) {
		return localStorage.getItem(name);
	},

	setLocalStorage: function(name, value) {
		try {
			localStorage.setItem(name, value);
		} catch (e) {
			console.log('localStorage cache to limit!');
		}
	},

	removeLocalStorage: function(key) {
		localStorage.removeItem(key);
	},

	setSessionStorage: function(name, value) {
		try {
			sessionStorage.setItem(name, value);
		} catch (e) {
			console.log('sessionStorage cache to limit!');
		}
	},

	getSessionStorage: function(name) {
		return sessionStorage.getItem(name);
	},

	removeSessionStoreage: function(key) {
		sessionStorage.removeItem(key);
	},

	queryString: function(item) {
		var svalue = location.search.match(new RegExp("[\?\&]" + item + "=([^\&]*)(\&?)", "i"));
		return svalue ? svalue[1] : svalue;
	},

	getObjSessionStorage: function(key) {
		return JSON.parse(sessionStorage.getItem(key));
	},

	getObjLocalStorage: function(key) {
		return JSON.parse(localStorage.getItem(key));
	},

	isEmail: function(email) {
		var bool = false,
			filter = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
		if (filter.test(email)) {
			bool = true;
		}
		return bool;
	},

	getBrowser: function() {
		let ua = window.navigator.userAgent,
			isIE = window.ActiveXObject != undefined && ua.indexOf("MSIE") != -1,
			isFirefox = ua.indexOf("Firefox") != -1,
			isOpera = window.opr != undefined,
			isChrome = ua.indexOf("Chrome") && window.chrome,
			isSafari = ua.indexOf("Safari") != -1 && ua.indexOf("Version") != -1;
		if (isIE) {
			return "IE";
		} else if (isFirefox) {
			return "Firefox";
		} else if (isOpera) {
			return "Opera";
		} else if (isChrome) {
			return "Chrome";
		} else if (isSafari) {
			return "Safari";
		} else {
			return "Unkown";
		}
	},

	isMobile: function() {
		// var bool = true;
		// if (!navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i)) {
		//     bool = false;
		// }
		var isSupportTouch = "ontouchend" in document ? true : false;
		return isSupportTouch;
	},

	subString: function(str, length) {
		str = (str.length > length) ? str.substring(0, length) + '...' : str;
		return str;
	},

	randomWord: function(randomFlag, min, max) {
		var str = "",
			pos,
			range = min,
			arr = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
		if (randomFlag) {
			range = Math.round(Math.random() * (max - min)) + min;
		}
		for (var i = 0; i < range; i++) {
			pos = Math.round(Math.random() * (arr.length - 1));
			str += arr[pos];
		}
		return str;
	}
}

export default tools;
